<?php header('content-type:text/html;charset=utf-8');
include 'conn.inc.php';
//print_r($_POST);
//Array ( [username] => aa [tel] => bb [email] => cc [content] => dd [submit] => enter-submit )

function getIp() {
	if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		$ip = getenv("HTTP_CLIENT_IP");
	else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
	else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
		$ip = getenv("REMOTE_ADDR");
	else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
		$ip = $_SERVER['REMOTE_ADDR'];
	else
		$ip = "unknown";
	return ($ip);
}

function GetClientIp($type = 0,$adv=false){
	$type =  $type ? 1 : 0;
	static $ip  = NULL;
	if ($ip !== NULL){
		return $ip[$type];
	}
	if($adv){
		if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
			$pos    =   array_search('unknown',$arr);
			if(false !== $pos){
				unset($arr[$pos]);	
			}
			$ip = trim($arr[0]);
		}elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		}elseif(isset($_SERVER['REMOTE_ADDR'])){
			$ip = $_SERVER['REMOTE_ADDR'];
		}
	}elseif(isset($_SERVER['REMOTE_ADDR'])){
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	// IP地址合法验证
	$long = sprintf("%u",ip2long($ip));
	$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
	return $ip[$type];
}
//echo GetClientIp();
if(empty($_POST['content'])){
die('({"eve":"0"})');
}

//$query = 'select gid,cid,gname,object,remark,path from groups where cid = ? and isshow = 1';
$query = 'insert into bbs(uname,tel,email,content,ip) values(?,?,?,?,?)';
try{
	$stmt = $link->prepare($query);
	$stmt->execute([$_POST['username'],$_POST['tel'],$_POST['email'],$_POST['content'],GetClientIp()]);
	die('({"eve":"1"})');
}catch(PDOException $e){
	die('获取小组出错:'.$e->getMessage());
}
?>
